package edu.scau.mis.rest.service.impl;

import edu.scau.mis.core.redis.RedisCache;
import edu.scau.mis.rest.entity.RestItem;
import edu.scau.mis.rest.entity.RestOrderItem;
import edu.scau.mis.rest.mapper.RestOrderItemMapper;
import edu.scau.mis.rest.service.RestOrderItemService;
import edu.scau.mis.rest.service.RestOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (RestOrderItem)表服务实现类
 *
 * @author makejava
 * @since 2022-03-16 11:18:28
 */
@Service("restOrderItemService")
public class RestOrderItemServiceImpl implements RestOrderItemService {
    @Resource
    private RestOrderItemMapper restOrderItemMapper;

    @Autowired
    RedisCache redisCache;

    @Autowired
    RestOrderService restOrderService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RestOrderItem queryById(Long id) {
        return this.restOrderItemMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param restOrderItem 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RestOrderItem> queryByPage(RestOrderItem restOrderItem, PageRequest pageRequest) {
        long total = this.restOrderItemMapper.count(restOrderItem);
        return new PageImpl<>(this.restOrderItemMapper.queryAllByLimit(restOrderItem, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param restOrderItem 实例对象
     * @return 实例对象
     */
    @Override
    public RestOrderItem insert(RestOrderItem restOrderItem) {
        this.restOrderItemMapper.insert(restOrderItem);
        return restOrderItem;
    }

    /**
     * 修改数据
     *
     * @param restOrderItem 实例对象
     * @return 实例对象
     */
    @Override
    public RestOrderItem update(RestOrderItem restOrderItem) {
        this.restOrderItemMapper.update(restOrderItem);
        return this.queryById(restOrderItem.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.restOrderItemMapper.deleteById(id) > 0;
    }

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param openid
     * @return 影响行数
     */
    @Override
    public int insertBatch(String openid){

        //获取餐位号的key
        String seat = redisCache.getListByIndex(openid, 0).toString();
        //获取订单id
        long orderId = (long) redisCache.getListByIndex(openid, 3);

        List<RestOrderItem> restOrderItemList = new ArrayList<>();

        for(long i = 0; i < redisCache.getListLength(seat); i++){
            RestItem restItem = (RestItem)redisCache.getListByIndex(seat, i);
            RestOrderItem restOrderItem = new RestOrderItem();
            restOrderItem.setItemId(restItem.getId());
            restOrderItem.setOrderId(orderId);
            restOrderItem.setSaleQuantity(restItem.getNum());
            restOrderItem.setSaleAmount(restItem.getSaleAmount());
            restOrderItemList.add(restOrderItem);
        }

        return this.restOrderItemMapper.insertBatch(restOrderItemList);
    }
}
